﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using mock_ehr_lib.Data;
using mock_ehr_lib.Model;
using mock_ehr_lib.View;

namespace mock_ehr_lib
{
    public partial class Login : Form
    {
        public Login()
        {
            InitializeComponent();
            Closed += loginClosed;
            btnLogin.Click += login;
        }

        void loginClosed(object sender, EventArgs e)
        {
            if(MockEHRApplication.Instance.CurrentUser == null)
                Application.Exit();
        }

        void login(object sender, EventArgs e)
        {
            string username = txtbxUsername.Text.ToLower();
            string password = txtbxPassword.Text.ToLower();
            var user = MockEHRApplication.Instance.UserRepository.GetAllUsers().Where(i => i.Username == username && i.Password == password).FirstOrDefault();
            if(user == null)
            {
                MessageBox.Show("Login failed.");
                return;
            }
            MockEHRApplication.Instance.CurrentUser = user;
            var form = new Main();
            form.Show();
            Close();
        }
    }
}
